import os, pygame, time, sys, math
from pygame.locals import *
import random

from CardImages import CardImages
from Card import Card

class CribTest:

    def __init__(self):
        print "Called __init__"

        # Initialize Screen
        pygame.init()
        self.screen = pygame.display.set_mode((640, 480), HWSURFACE|RESIZABLE)
        pygame.display.set_caption('Cribbage Test - v1')

        # Create CribTest attributes
        self.imageFactory = CardImages()

    def drawCard(self, card):
        """Card draws itself on the screen."""
        self.screen.blit(self.imageFactory.getCard(card), card.getCoord())
        # TODO: check whether card is showing or not, draw front or back

    def mainLoop(self):
        print "Called mainLoop"

        self.screen.fill((0,0,0)) # Black background

        eddiesCard = Card('a','s', True, 250, 50) # Ace of Spades
        self.drawCard(eddiesCard)

        ranks = ['a','2','3','4','5','6','7','8','9','t','j','q','k']
        suits = ['s','c','d','h']

        wholeDeck=[]

        for r in range(len(ranks)):
            for s in range(len(suits)):
                wholeDeck.append(Card(ranks[r], suits[s], True))

        for i in range(10):

            random.shuffle(wholeDeck)
            
            for r in range(len(ranks)):
                for s in range(len(suits)):
                   xval = r * 30
                   yval = s * 100 + r*2
                   wholeDeck[s*13+r].x = xval
                   wholeDeck[s*13+r].y = yval          
                   
            for i in range(len(wholeDeck)):
                time.sleep(0.02)
                self.drawCard(wholeDeck[i])
                pygame.display.flip()

            print str(pygame.event.get()) # Toss all current events
            while pygame.event.get(MOUSEBUTTONDOWN) == []:
                time.sleep(0.2)





        print "Finished mainLoop"

def main():
    print "Start main()"
    g = CribTest()
    g.mainLoop()
    print "End main()"

    #Quit everything
    pygame.quit()
    sys.exit()

#this calls the 'main' function when this script is executed
if __name__ == '__main__': main()
